using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TemplateWizard;
using System.Windows.Forms;
using EnvDTE;
using EnvDTE80;
using SPVisualDev.VSAddin.Dialogs;

namespace SPVisualDev.VSAddin
{
    public class NewSPArtifactsProject: IWizard
    {
        private DTE2 m_DTE;
        private string m_project_name;

        public NewSPArtifactsProject()
        {
        }

        // This method is called before opening any item that 
        // has the OpenInEditor attribute.
        public void BeforeOpeningFile(ProjectItem projectItem)
        {
        }

        public void ProjectFinishedGenerating(Project project)
        {

            try
            {
                Common.SetProjectGlobalConfigKey(project, Common.C_PROJECT_ID_VAR_NAME, Common.C_SHAREPOINT_ARTIFACTS_PROJECT_ID, true);
                Common.SetProjectGlobalConfigKey(project, "ASPXPagesNSsuffix", "ApplicationPages", true);
                Common.SetProjectGlobalConfigKey(project, "WebCustomConstrolNSsuffix", "UI.WebControls", true);
                Common.SetProjectGlobalConfigKey(project, "MasterPagesNSsuffix", "MasterPages", true);
                Common.SetProjectGlobalConfigKey(project, "WebPartsNSsuffix", "UI.WebParts", true);
                Common.SetProjectGlobalConfigKey(project, "EventRecNSsuffix", "EventReceivers", true);
                Common.SetProjectGlobalConfigKey(project, "WSPSolutionId", Guid.NewGuid().ToString(), true);

                /*
                Common.SetProjectGlobalConfigKey(project, "EventRecCodeInFeatureFolder", "True", true);
                Common.SetProjectGlobalConfigKey(project, "WPCodeInFeatureFolder", "True", true);
                */


                object extender = project.get_Extender("WebApplication");// as Microsoft.VisualStudio.Web.Application.WAProjectExtender;
                extender.GetType().GetProperty("StartWebServerOnDebug").SetValue(extender, false, null);
                extender.GetType().GetProperty("UseIIS").SetValue(extender, false, null);

                //extender.StartWebServerOnDebug = false;
                //extender.UseIIS = false;

                try
                {
                    ProjectItem item = project.ProjectItems.Item("12");
                    item.ProjectItems.Item("TEMPLATE").ExpandView();
                }
                catch { }


                Connect addin = m_DTE.AddIns.Item(typeof(Connect).FullName).Object as Connect;
                try
                {
                    if (addin != null)
                    {
                        UIHierarchyItem item = m_DTE.ToolWindows.SolutionExplorer.GetItem(project.Name);
                        if (item != null)
                            item.Select(vsUISelectionType.vsUISelectionTypeSelect);

                        addin._selection_events_OnChange();
                    }
                }
                catch { }

                try
                {
                    if (!Connect.S_Client_Install)
                    {

                        VSLangProj.VSProject vsProject = project.Object as VSLangProj.VSProject;

                        vsProject.References.Add("Microsoft.Sharepoint");
                        vsProject.References.Add("Microsoft.Sharepoint.Security");

                        project.Save(project.FullName);
                    }
                }
                catch (Exception ex)
                {
                    DialogError.ShowError(ex, "Failed to add references to Microsoft SharaPoint dlls.", false);
                }

                MessageBox.Show(@"You have now successfully created a new project with default settings. You will now be redirected to the configuration screen where you can change settings for the project.

More information about the various settings can be found in the product documentation. When you are ready configuring the project click 'save' and you will be able to start working with your project.

You can return anytime to the configuration dialog by right-clicking the project node and then go to SPVisualDev --> Atifact config.", "Project succefully created", MessageBoxButtons.OK, MessageBoxIcon.Information);

//                Connect connect = project.DTE.AddIns.Item("SPVisualDev.Connect").Object as Connect;
                addin.ArtifactConfig(project);

            }
            catch (Exception ex)
            {
                DialogError.ShowError(ex);

                throw new WizardBackoutException(ex.ToString());
            }        
        }

        // This method is only called for item templates,
        // not for project templates.
        public void ProjectItemFinishedGenerating(ProjectItem projectItem)
        {
        }

        // This method is called after the project is created.
        public void RunFinished()
        {
        }

        public void RunStarted(object automationObject,
            Dictionary<string, string> replacementsDictionary,
            WizardRunKind runKind, object[] customParams)
        {
            try
            {
                m_DTE = (DTE2)automationObject;

                m_project_name = replacementsDictionary["$projectname$"];
            }
            catch (Exception ex)
            {
                throw new WizardBackoutException(ex.ToString());
            }
        }

        // This method is only called for item templates,
        // not for project templates.
        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }
    }
}
